package com.zzyl.serve.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.mapper.NursingProjectPlanMapper;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;

/**
 * 护理计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    @Override
    public NursingPlanVo selectNursingPlanById(Long id)
    {
        // 1.查询护理计划表
        NursingPlan nursingPlan = nursingPlanMapper.selectNursingPlanById(id);
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan,nursingPlanVo);
        //2.根据护理计划id查询关联的所有护理项目
        List<NursingProjectPlanVo>list = nursingProjectPlanMapper.selectByplanId(id);
        nursingPlanVo.setProjectPlans(list);



        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     * 
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    @Transactional//事务
    public int insertNursingPlan(NursingPlanDto dto)
    {
       //1.插入护理计划表
        NursingPlan nursingPlan= new NursingPlan() ;
        BeanUtils.copyProperties(dto,nursingPlan);
        save(nursingPlan);

        //2.批量插入中间表
        List<NursingProjectPlan> projectPlans = dto.getProjectPlans();
        int i = nursingProjectPlanMapper.batchInsert(nursingPlan.getId(), projectPlans);


        return i;
    }

    /**
     * 修改护理计划
     * 
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int updateNursingPlan(NursingPlanDto dto)
    {
        //1.对象拷贝
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto,nursingPlan);
        nursingPlan.setId(Long.valueOf(dto.getId()+""));
        //2.判断dto中的护理项目列表受否为空
        List<NursingProjectPlan> projectPlans = dto.getProjectPlans();
        // 3.如果为空，则直接修改护理计划表
        if (!CollectionUtils.isEmpty(projectPlans)){
            nursingProjectPlanMapper.deleteByPlanId(nursingPlan.getId());
            nursingProjectPlanMapper.batchInsert(nursingPlan.getId(), projectPlans);

        }
        // 如果不为空，则先删除中间表数据，再批量插入


        return updateById(nursingPlan) ? 1 : 0;
    }

    /**
     * 批量删除护理计划
     * 
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id)
    {


        // 删除护理计划与护理项目的关系
        nursingProjectPlanMapper.deleteByPlanId(id);
        // 删除护理计划
        return removeById(id) ? 1 : 0;
    }
}
